using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class LoadGif : MonoBehaviour
{
    private RawImage image;
    private List<UniGif.GifTexture> textureList;

    // 当前播放的索引
    int index = -1;
    float timer = 0;
    void Start()
    {
        image = GetComponent<RawImage>();
        string filePath = Application.dataPath + "/test.gif";
        Stream stream = new FileStream(filePath,FileMode.Open,FileAccess.Read);
        byte[] bytes = new byte[stream.Length];
        stream.Read(bytes,0,(int)stream.Length);
        StartCoroutine(UniGif.GetTextureListCoroutine(bytes,LoadGifHandler));
    }

    private void Update() {
        if(index == -1){
            return;
        }
        timer += Time.deltaTime;
        if(timer >= 0.05f){
            timer = 0;
            image.texture = textureList[index++].m_texture2d;
            if(index >= textureList.Count){
                index = 0;
            }
        }
    }

    void LoadGifHandler(List<UniGif.GifTexture> list,int count,int width,int height){
        textureList = list;
        index = 0;
    }
}
